const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const UserAddress = sequelize.define('UserAddress', {
  id: { 
    type: DataTypes.INTEGER.UNSIGNED, 
    primaryKey: true, 
    autoIncrement: true 
  },
  userId: { 
    type: DataTypes.INTEGER.UNSIGNED, 
    allowNull: false, 
    field: 'user_id' 
  },
  receiverName: { 
    type: DataTypes.STRING(50), 
    allowNull: false, 
    field: 'receiver_name' 
    // 收货人姓名
  },
  receiverPhone: { 
    type: DataTypes.STRING(20), 
    allowNull: false, 
    field: 'receiver_phone' 
    // 收货人手机
  },
  provinceName: { 
    type: DataTypes.STRING(20), 
    allowNull: false, 
    field: 'province_name' 
    // 省
  },
  cityName: { 
    type: DataTypes.STRING(20), 
    allowNull: false, 
    field: 'city_name' 
    // 市
  },
  districtName: { 
    type: DataTypes.STRING(20), 
    allowNull: false, 
    field: 'district_name' 
    // 区/县
  },
  provinceCode: { 
    type: DataTypes.STRING(20), 
    allowNull: true, 
    field: 'province_code' 
    // 省编码
  },
  cityCode: { 
    type: DataTypes.STRING(20), 
    allowNull: true, 
    field: 'city_code' 
    // 市编码
  },
  districtCode: { 
    type: DataTypes.STRING(20), 
    allowNull: true, 
    field: 'district_code' 
    // 区/县编码
  },
  detail: { 
    type: DataTypes.STRING(255), 
    allowNull: false, 
    field: 'detail' 
    // 详细地址
  },
  defaultAddress: { 
    type: DataTypes.TINYINT, 
    allowNull: false, 
    defaultValue: 0, 
    field: 'is_default' 
    // 是否默认：1默认 0非默认
  }
}, {
  tableName: 'user_address',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at'
});

// 添加实例方法以确保字段正确序列化
UserAddress.prototype.toJSON = function() {
  const values = Object.assign({}, this.get());
  // 移除时间字段
  delete values.created_at;
  delete values.updated_at;
  return values;
};

module.exports = UserAddress;